{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "MIRNet-Low-Light-Train.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyPYkiW3WGoZrgLnaTdb98k7",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/soumik12345/MIRNet/blob/master/notebooks/MIRNet_Low_Light_Train.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "u6SxYHy-0tAX",
        "outputId": "0b8689e6-a29e-4bd0-9f6f-f4d81cc7f836"
      },
      "source": [
        "!nvidia-smi"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Tue Dec  1 07:16:36 2020       \n",
            "+-----------------------------------------------------------------------------+\n",
            "| NVIDIA-SMI 455.38       Driver Version: 418.67       CUDA Version: 10.1     |\n",
            "|-------------------------------+----------------------+----------------------+\n",
            "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
            "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
            "|                               |                      |               MIG M. |\n",
            "|===============================+======================+======================|\n",
            "|   0  Tesla T4            Off  | 00000000:00:04.0 Off |                    0 |\n",
            "| N/A   66C    P8    11W /  70W |      0MiB / 15079MiB |      0%      Default |\n",
            "|                               |                      |                 ERR! |\n",
            "+-------------------------------+----------------------+----------------------+\n",
            "                                                                               \n",
            "+-----------------------------------------------------------------------------+\n",
            "| Processes:                                                                  |\n",
            "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
            "|        ID   ID                                                   Usage      |\n",
            "|=============================================================================|\n",
            "|  No running processes found                                                 |\n",
            "+-----------------------------------------------------------------------------+\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "H42UtLtD3QMh",
        "outputId": "9e3d5c8d-7f11-40c6-a0c5-a1d2ddbcc0c0"
      },
      "source": [
        "!git clone https://github.com/soumik12345/MIRNet\n",
        "%cd MIRNet"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'MIRNet'...\n",
            "remote: Enumerating objects: 138, done.\u001b[K\n",
            "remote: Counting objects: 100% (138/138), done.\u001b[K\n",
            "remote: Compressing objects: 100% (104/104), done.\u001b[K\n",
            "remote: Total 138 (delta 52), reused 106 (delta 25), pack-reused 0\u001b[K\n",
            "Receiving objects: 100% (138/138), 9.84 MiB | 14.21 MiB/s, done.\n",
            "Resolving deltas: 100% (52/52), done.\n",
            "/content/MIRNet\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GMhYQqQ_6Qh5",
        "outputId": "f263304a-bb43-458d-e06f-b345cc496f60"
      },
      "source": [
        "!pip install -qq wandb"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\u001b[K     |████████████████████████████████| 1.8MB 23.2MB/s \n",
            "\u001b[K     |████████████████████████████████| 102kB 14.7MB/s \n",
            "\u001b[K     |████████████████████████████████| 133kB 60.6MB/s \n",
            "\u001b[K     |████████████████████████████████| 102kB 13.9MB/s \n",
            "\u001b[K     |████████████████████████████████| 163kB 59.2MB/s \n",
            "\u001b[K     |████████████████████████████████| 71kB 11.6MB/s \n",
            "\u001b[?25h  Building wheel for watchdog (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Building wheel for subprocess32 (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Building wheel for pathtools (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hAXrPnAx-seN"
      },
      "source": [
        "from glob import glob\n",
        "import tensorflow as tf\n",
        "from mirnet.train import LowLightTrainer\n",
        "from mirnet.utils import init_wandb, download_dataset\n",
        "\n",
        "\n",
        "tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fT7Fr8Tq_bqn",
        "outputId": "96ccfa74-77e8-453d-d6c1-7227387daea2"
      },
      "source": [
        "download_dataset('LOL')"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading dataset...\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "Downloading...\n",
            "From: https://drive.google.com/uc?id=157bjO1_cFuSd0HWDUuAmcHRJDVyWpOxB\n",
            "To: /content/MIRNet/LOLdataset.zip\n",
            "347MB [00:01, 211MB/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Unpacking Dataset\n",
            "Done!!!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "id": "0lO57o2W_sqq",
        "outputId": "ac006479-bf25-428f-e087-7ff523e89bba"
      },
      "source": [
        "init_wandb(\n",
        "    project_name='mirnet',\n",
        "    experiment_name='LOL_lowlight_experiment_2_256x256',\n",
        "    wandb_api_key='cf0947ccde62903d4df0742a58b8a54ca4c11673'\n",
        ")"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33m19soumik-rakshit96\u001b[0m (use `wandb login --relogin` to force relogin)\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "\n",
              "                Tracking run with wandb version 0.10.11<br/>\n",
              "                Syncing run <strong style=\"color:#cdcd00\">LOL_lowlight_experiment_2_256x256</strong> to <a href=\"https://wandb.ai\" target=\"_blank\">Weights & Biases</a> <a href=\"https://docs.wandb.com/integrations/jupyter.html\" target=\"_blank\">(Documentation)</a>.<br/>\n",
              "                Project page: <a href=\"https://wandb.ai/19soumik-rakshit96/mirnet\" target=\"_blank\">https://wandb.ai/19soumik-rakshit96/mirnet</a><br/>\n",
              "                Run page: <a href=\"https://wandb.ai/19soumik-rakshit96/mirnet/runs/15w0gpyv\" target=\"_blank\">https://wandb.ai/19soumik-rakshit96/mirnet/runs/15w0gpyv</a><br/>\n",
              "                Run data is saved locally in <code>/content/MIRNet/wandb/run-20201201_071736-15w0gpyv</code><br/><br/>\n",
              "            "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Kiom1ZjH8eKi"
      },
      "source": [
        "trainer = LowLightTrainer()\n",
        "train_low_light_images = glob('./our485/low/*')\n",
        "train_high_light_images = glob('./our485/high/*')\n",
        "valid_low_light_images = glob('./eval15/low/*')\n",
        "valid_high_light_images = glob('./eval15/high/*')"
      ],
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Kz0i4QoQ8qw1"
      },
      "source": [
        "trainer.build_dataset(\n",
        "    train_low_light_images, train_high_light_images,\n",
        "    valid_low_light_images, valid_high_light_images,\n",
        "    crop_size=256, batch_size=2\n",
        ")\n",
        "trainer.compile()"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zeCmSZqI9GBU",
        "outputId": "e939e100-091d-4596-c542-1516c60f401f"
      },
      "source": [
        "trainer.train(epochs=100, checkpoint_dir='./checkpoints')"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 1/100\n",
            "243/243 [==============================] - 442s 2s/step - loss: 0.1592 - psnr: 64.1155 - val_loss: 0.1249 - val_psnr: 65.5065\n",
            "Epoch 2/100\n",
            "243/243 [==============================] - 433s 2s/step - loss: 0.1509 - psnr: 64.5416 - val_loss: 0.1429 - val_psnr: 64.6090\n",
            "Epoch 3/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.1439 - psnr: 65.2337 - val_loss: 0.1303 - val_psnr: 65.2745\n",
            "Epoch 4/100\n",
            "243/243 [==============================] - 436s 2s/step - loss: 0.1285 - psnr: 65.8450 - val_loss: 0.1140 - val_psnr: 66.2238\n",
            "Epoch 5/100\n",
            "243/243 [==============================] - 438s 2s/step - loss: 0.1194 - psnr: 66.4395 - val_loss: 0.1135 - val_psnr: 66.3191\n",
            "Epoch 6/100\n",
            "243/243 [==============================] - 437s 2s/step - loss: 0.1102 - psnr: 67.0214 - val_loss: 0.1049 - val_psnr: 66.9083\n",
            "Epoch 7/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.1060 - psnr: 67.3232 - val_loss: 0.1132 - val_psnr: 66.4320\n",
            "Epoch 8/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.1069 - psnr: 67.3737 - val_loss: 0.1094 - val_psnr: 66.4070\n",
            "Epoch 9/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.1044 - psnr: 67.4384 - val_loss: 0.1210 - val_psnr: 65.9124\n",
            "Epoch 10/100\n",
            "243/243 [==============================] - 432s 2s/step - loss: 0.1080 - psnr: 67.3665 - val_loss: 0.1115 - val_psnr: 66.9443\n",
            "Epoch 11/100\n",
            "243/243 [==============================] - 438s 2s/step - loss: 0.1041 - psnr: 67.5550 - val_loss: 0.1022 - val_psnr: 67.0890\n",
            "Epoch 12/100\n",
            "243/243 [==============================] - 432s 2s/step - loss: 0.1043 - psnr: 67.4996 - val_loss: 0.1067 - val_psnr: 67.0900\n",
            "Epoch 13/100\n",
            "243/243 [==============================] - 438s 2s/step - loss: 0.1059 - psnr: 67.4166 - val_loss: 0.0977 - val_psnr: 67.6418\n",
            "Epoch 14/100\n",
            "243/243 [==============================] - 438s 2s/step - loss: 0.0989 - psnr: 67.9418 - val_loss: 0.0963 - val_psnr: 68.1703\n",
            "Epoch 15/100\n",
            "243/243 [==============================] - 438s 2s/step - loss: 0.1015 - psnr: 67.7641 - val_loss: 0.0939 - val_psnr: 68.4126\n",
            "Epoch 16/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0982 - psnr: 67.9785 - val_loss: 0.0951 - val_psnr: 68.0276\n",
            "Epoch 17/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0981 - psnr: 68.0821 - val_loss: 0.1169 - val_psnr: 66.7316\n",
            "Epoch 18/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.1004 - psnr: 67.8817 - val_loss: 0.1016 - val_psnr: 67.3338\n",
            "Epoch 19/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0959 - psnr: 68.2082 - val_loss: 0.0995 - val_psnr: 67.4730\n",
            "Epoch 20/100\n",
            "243/243 [==============================] - ETA: 0s - loss: 0.1026 - psnr: 67.7257\n",
            "Epoch 00020: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-05.\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.1026 - psnr: 67.7257 - val_loss: 0.1003 - val_psnr: 67.3352\n",
            "Epoch 21/100\n",
            "243/243 [==============================] - 436s 2s/step - loss: 0.0919 - psnr: 68.6314 - val_loss: 0.0826 - val_psnr: 69.3167\n",
            "Epoch 22/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0936 - psnr: 68.3714 - val_loss: 0.1035 - val_psnr: 67.5037\n",
            "Epoch 23/100\n",
            "243/243 [==============================] - 432s 2s/step - loss: 0.0896 - psnr: 68.7174 - val_loss: 0.0862 - val_psnr: 69.1280\n",
            "Epoch 24/100\n",
            "243/243 [==============================] - 435s 2s/step - loss: 0.0917 - psnr: 68.6044 - val_loss: 0.0796 - val_psnr: 69.3943\n",
            "Epoch 25/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0895 - psnr: 68.7495 - val_loss: 0.0898 - val_psnr: 68.8661\n",
            "Epoch 26/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0890 - psnr: 68.8537 - val_loss: 0.0983 - val_psnr: 68.0671\n",
            "Epoch 27/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0920 - psnr: 68.5267 - val_loss: 0.0975 - val_psnr: 67.9822\n",
            "Epoch 28/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0897 - psnr: 68.7615 - val_loss: 0.1008 - val_psnr: 67.6455\n",
            "Epoch 29/100\n",
            "243/243 [==============================] - ETA: 0s - loss: 0.0864 - psnr: 69.0921\n",
            "Epoch 00029: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-05.\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0864 - psnr: 69.0921 - val_loss: 0.0940 - val_psnr: 68.5634\n",
            "Epoch 30/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0868 - psnr: 69.0637 - val_loss: 0.0917 - val_psnr: 68.4715\n",
            "Epoch 31/100\n",
            "243/243 [==============================] - 436s 2s/step - loss: 0.0845 - psnr: 69.2952 - val_loss: 0.0791 - val_psnr: 69.2714\n",
            "Epoch 32/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0867 - psnr: 69.1577 - val_loss: 0.0809 - val_psnr: 69.5186\n",
            "Epoch 33/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0860 - psnr: 69.3142 - val_loss: 0.0868 - val_psnr: 68.9742\n",
            "Epoch 34/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0858 - psnr: 69.1895 - val_loss: 0.0890 - val_psnr: 68.3421\n",
            "Epoch 35/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0860 - psnr: 69.1920 - val_loss: 0.0791 - val_psnr: 69.2999\n",
            "Epoch 36/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0867 - psnr: 69.2137 - val_loss: 0.0832 - val_psnr: 68.8973\n",
            "Epoch 37/100\n",
            "243/243 [==============================] - 436s 2s/step - loss: 0.0853 - psnr: 69.2414 - val_loss: 0.0716 - val_psnr: 70.2069\n",
            "Epoch 38/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0858 - psnr: 69.2487 - val_loss: 0.0895 - val_psnr: 68.9241\n",
            "Epoch 39/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0856 - psnr: 69.2450 - val_loss: 0.0896 - val_psnr: 68.6078\n",
            "Epoch 40/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0840 - psnr: 69.2957 - val_loss: 0.0752 - val_psnr: 69.8205\n",
            "Epoch 41/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0843 - psnr: 69.3408 - val_loss: 0.0924 - val_psnr: 68.4360\n",
            "Epoch 42/100\n",
            "243/243 [==============================] - ETA: 0s - loss: 0.0834 - psnr: 69.4409\n",
            "Epoch 00042: ReduceLROnPlateau reducing learning rate to 1.249999968422344e-05.\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0834 - psnr: 69.4409 - val_loss: 0.0966 - val_psnr: 67.8450\n",
            "Epoch 43/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0803 - psnr: 69.8295 - val_loss: 0.0844 - val_psnr: 69.0862\n",
            "Epoch 44/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0801 - psnr: 69.7533 - val_loss: 0.0876 - val_psnr: 68.7879\n",
            "Epoch 45/100\n",
            "243/243 [==============================] - 431s 2s/step - loss: 0.0826 - psnr: 69.6043 - val_loss: 0.0934 - val_psnr: 68.2593\n",
            "Epoch 46/100\n",
            "243/243 [==============================] - 430s 2s/step - loss: 0.0789 - psnr: 69.9293 - val_loss: 0.0947 - val_psnr: 68.2925\n",
            "Epoch 47/100\n",
            "243/243 [==============================] - ETA: 0s - loss: 0.0803 - psnr: 69.7273\n",
            "Epoch 00047: ReduceLROnPlateau reducing learning rate to 6.24999984211172e-06.\n",
            "243/243 [==============================] - 429s 2s/step - loss: 0.0803 - psnr: 69.7273 - val_loss: 0.0935 - val_psnr: 68.1041\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tensorflow.python.keras.callbacks.History at 0x7f173bebcc88>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "dGkCwyM2_odJ",
        "outputId": "451d95d4-8113-4322-fbbb-6f4b40c0898a"
      },
      "source": [
        "from glob import glob\n",
        "from google.colab import files\n",
        "\n",
        "\n",
        "for file in glob('/content/MIRNet/checkpoints/*'):\n",
        "    files.download(file)"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "\n",
              "    async function download(id, filename, size) {\n",
              "      if (!google.colab.kernel.accessAllowed) {\n",
              "        return;\n",
              "      }\n",
              "      const div = document.createElement('div');\n",
              "      const label = document.createElement('label');\n",
              "      label.textContent = `Downloading \"${filename}\": `;\n",
              "      div.appendChild(label);\n",
              "      const progress = document.createElement('progress');\n",
              "      progress.max = size;\n",
              "      div.appendChild(progress);\n",
              "      document.body.appendChild(div);\n",
              "\n",
              "      const buffers = [];\n",
              "      let downloaded = 0;\n",
              "\n",
              "      const channel = await google.colab.kernel.comms.open(id);\n",
              "      // Send a message to notify the kernel that we're ready.\n",
              "      channel.send({})\n",
              "\n",
              "      for await (const message of channel.messages) {\n",
              "        // Send a message to notify the kernel that we're ready.\n",
              "        channel.send({})\n",
              "        if (message.buffers) {\n",
              "          for (const buffer of message.buffers) {\n",
              "            buffers.push(buffer);\n",
              "            downloaded += buffer.byteLength;\n",
              "            progress.value = downloaded;\n",
              "          }\n",
              "        }\n",
              "      }\n",
              "      const blob = new Blob(buffers, {type: 'application/binary'});\n",
              "      const a = document.createElement('a');\n",
              "      a.href = window.URL.createObjectURL(blob);\n",
              "      a.download = filename;\n",
              "      div.appendChild(a);\n",
              "      a.click();\n",
              "      div.remove();\n",
              "    }\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "download(\"download_6ea7b0ed-1fe2-4f54-a838-3f3f7d13aa3c\", \"low_light_weights_best_256x256.h5\", 147825648)"
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IrgoJL0xZTuv"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}